<template>
  <div class="app-container">
    <div class="zd3">
      <div class="zd3-title">
        <p>
          <span>单据号</span>
          <input v-model="searchData.danjuhao" type="text" placeholder="请输入单据号" />
        </p>
        <p>
          <span>单据日期</span>
          <input v-model="searchData.danjuriqi" type="text" placeholder="请输入单据日期" />
        </p>
        <p>
          <span>业务员</span>
          <input v-model="searchData.yewuyuan" type="text" placeholder="请输入业务员" />
        </p>
        <p>
          <span>收获客户</span>
          <input v-model="searchData.shouhuokehu" type="text" placeholder="请输入收获客户" />
        </p>
        <el-button size="small" type="success" @click="getlist">查询</el-button>
        <el-button size="small" @click="resetList">重置</el-button>
        <el-upload
          class="upload-demo"
          action=""
          :show-file-list="false"
          :http-request="designUpFile"
          :multiple='false'
          >
          <el-button size="small" type="primary">导入</el-button>
        </el-upload>
        <el-button size="small" type="primary" @click="addHanlder">新增</el-button>
        <el-button size="small" type="primary" @click="jisuanHanlder" :loading="isClick">批量计算</el-button>
        <el-button size="small" type="danger" @click="deletesHanlder" :loading="hanlderPl">批量删除</el-button>
      </div>
      <div class="zd3-btm">
        <el-table
          ref="multipleTable"
          :data="tableData"
          style="width: 100%;border-radius: 8px;"
          :show-header="showHeader"
          v-loading="loading"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column align="center" prop="danjuhao" label="单据号" />
          <el-table-column align="center" prop="danjuriqi" label="单据日期" />
          <el-table-column align="center" prop="yewuyuan" label="业务员" />
          <el-table-column align="center" prop="shouhuokehu" label="收货客户" />
          <el-table-column align="center" prop="wuliaomingcheng" label="物料名称" />
          <el-table-column align="center" prop="bzw" label="包装物" />
          <el-table-column align="center" prop="shuliang" label="数量" />
          <el-table-column align="center" prop="hanshuidanjia" label="含税单价" />
          <el-table-column align="center" prop="jiashuiheji" label="价税合计" />
          <el-table-column align="center" prop="chanmofeiyong" label="打托缠膜" />
          <el-table-column align="center" prop="insertTime" label="导入时间" />
          <el-table-column align="center" width="200" label="操作">
            <template slot-scope="scope">
              <!-- <el-button type="text" size="small" @click="count(scope.row,scope.$index)">计算</el-button> -->
              <el-button type="text" size="small" @click="look(scope.row,scope.$index)">查看</el-button>
              <el-button type="text" size="small" @click="write(scope.row,scope.$index)">修改</el-button>
              <el-popconfirm
                placement="top"
                hideIcon
                title="是否要删除此行？"
                @onConfirm="deleteRow(scope.row)"
              >
                <el-button type="text" size="small" slot="reference">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <div class="pages">
          <p>{{tableData.length}} / 页</p>
          <el-pagination
            background
            align="right"
            :page-size="size"
            layout="prev, pager, next"
            :total="total"
            @current-change='goPage'
            @prev-click="goPrev"
            @next-click="goNext"
          />
        </div>
        
      </div>
    </div>
    <el-dialog
      :visible.sync="boxshow"
      :close-on-click-modal="false"
      :modal-append-to-body="false"
      :modal="true"
      width="65%"
    >
      <div class="change">
        <p>
          <span>包装物</span>
          <input type="text" v-model="tempData.baozhuangwu" placeholder="请输入包装物" />
        </p>
        <p>
          <span>包装价</span>
          <input type="text" v-model="tempData.buhanbaozhuang" placeholder="请输入包装价" />
        </p>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="childrenShow = false">取消</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { importFahuo,fahuoList,deletefahuo,jisuanxqlist,deletePlfahuo } from '@/api/neimao'
  export default {
    data(){
      return{
        total: 1,            //总页数
        page: 1,             //当前页数
        size: 50,            //每页个数
        showHeader: true,    //是否显示表头
        loading: true,       //表格数据加载
        tableData: [],       //表格数据
        tempData: {},        //中介数据
        searchData: {},       //搜索数据
        multipleSelection:[],  //多选数据
        boxshow: false,        //弹窗
        tempIndex: -1,
        isClick: false,   //批量计算
        hanlderPl: false,  //批量删除
      }
    },
    mounted(){
      this.getlist()
    },
    methods:{
      // 获取发货报表
      getlist(){
        this.loading = true
        this.searchData.currentPage  = this.page
        fahuoList(this.searchData).then(res => {
          console.log(res)
          if(res.data){
            this.tableData = res.data.records
            console.log(this.tableData)
            this.total = res.data.total
            this.size = res.data.size
            console.log(this.total)
            this.loading = false
          }else{
            this.loading = false
          }
        }).catch(res => {
          this.loading = false
        })
      },
      // 导入文件
      designUpFile(item){
        const loading = this.$loading({
          lock: true,
          text: '上传中请稍等...',
          spinner: 'el-icon-loading',
          background: 'rgba(255, 255, 255, 0.8)'
        })
        var formData = new FormData()
        formData.append("file", item.file)
        importFahuo(formData).then(res => {
          console.log(res)
          this.getlist()
          this.$notify({
            title: '成功',
            message: `成功导入${res.data}条数据！`,
            type: 'success',
            duration: 0
          });
          loading.close()
        }).catch(() => {
          loading.close()
        })
      },
      // 批量计算
      jisuanHanlder(){
        if(this.multipleSelection.length != 0){
          this.isClick = true
          const list = this.multipleSelection
          console.log(list)
          const str = JSON.stringify(list)
          jisuanxqlist(str).then(res => {
            console.log(res)
            if(res.data){
              this.$router.push({
                name: 'Jisuanneifahuo',
                params: {
                  options: JSON.stringify(res.data)
                }
              })
            }else{
              this.$notify({
                title: '注意',
                message: res.message,
                type: 'warning',
                duration: 0
              });
            }
            this.isClick = false
          }).catch(() => {
            this.isClick = false
          })
        }else{
          this.$message({
            message: '请先选择要结算的数据!',
            type: 'warning'
          })
        }
      },
      // 新增res
      addHanlder(item){
         this.$router.push({
          name: 'Writeneifahuo'
        })
      },
      resetList(){
        this.searchData = {}
      },
      // 计算
      count(res){
        console.log(res)
        this.$router.push({
          name: 'Jisuanneifahuo',
          params: {
            options: JSON.stringify(res)
          }
        })
      },
      deletesHanlder(res){
        console.log(this.multipleSelection)
        if(this.multipleSelection.length != 0){
          const arr = this.multipleSelection.map(item => item.id)
          console.log(arr)
          this.$confirm('您将批量删除文件, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.hanlderPl = true
            deletePlfahuo(arr).then(res => {
              console.log(res)
              this.getlist()
              this.$message({
                type: 'success',
                message: '批量删除成功！',
                showClose: true
              });
              this.hanlderPl = false
            }).catch(() => {
              this.hanlderPl = false
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            });          
          });
        }else{
          this.$message({
            message: '请先选择要删除的数据!',
            type: 'warning'
          })
        }
        console.log(res)
      },
      look(res){
        this.$router.push({
          name: 'Writeneifahuo',
          params: {
            options: JSON.stringify(res),
            look: true
          }
        })
      },
      // 修改
      write(res,index){
        console.log(res)
        this.$router.push({
          name: 'Writeneifahuo',
          params: {
            options: JSON.stringify(res)
          }
        })
      },
      // 强制计算
      forceCount(res){
        console.log(res)
      },
      // 删除
      deleteRow(res){
        console.log(res)
        deletefahuo(res.id).then(res => {
          console.log(res)
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.getlist()
        })
      },
      exportExcel() {
        this.downloadLoading = true
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = ['序号', '教室', '部门', '培训人', '电话', '培训内容', '开始时间', '结束时间']
          const filterVal = ['orderindex', 'jiaoshi', 'bumen', 'person', 'number', 'contents', 'starttime', 'endtime']
          const list = this.tableData
          const data = this.formatJson(filterVal, list)
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: '采购申请表',
            autoWidth: this.autoWidth,
            bookType: this.bookType
          })
          this.downloadLoading = false
        })
      },
      formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => {
          if (j === 'timestamp') {
            return parseTime(v[j])
          } else {
            return v[j]
          }
        }))
      },
      // 上一页
      goPrev(page) {
        console.log(page);
        this.loading = true
        this.page--
        this.getlist()
      },
      // 下一页
      goNext(page) {
        console.log(page);
        this.loading = true
        this.page++
        this.getlist()
      },
      // 跳转指定页
      goPage(page){
        this.loading = true
        this.page = page
        this.getlist()
      },
      // 多选
      handleSelectionChange(val) {
        this.multipleSelection = val;
        console.log(this.multipleSelection)
      },
      save(){
        if(this.tempIndex == -1){
          addFahuo(this.tempData).then(res => {
            console.log(res)
            this.$message({
              message: res.message,
              type: 'success'
            })
            this.boxshow = false
            this.getlist()
          })
        }else{
          updateFahuo(this.tempData).then(res => {
            console.log(res)
            this.$message({
              message: res.message,
              type: 'success'
            })
            this.boxshow = false
            this.getlist()
          })
        }
      }
    }
  }
</script>

<style scoped lang="scss">
/deep/.el-button--primary {
  color: #fff;
  background-color: #18a05e !important;
  border-color: #18a05e !important;
}
.pages{
  font-size: 14px;
  color: #333;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
.upload-demo{
  margin: 0 10px;
}
/deep/.el-button--primary:hover,
.el-button--primary:focus {
  background: #18a05e !important;
  border-color: #18a05e !important;
  color: #fff !important;
}
/deep/.el-button--primary {
  color: #fff;
  background-color: #18a05e;
  border-color: #18a05e;
}
/deep/.el-button--text {
  color: #00c666 !important;
}
/deep/.el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #18a05e;
  color: #fff;
}
.zd3-btm /deep/.btn-prev:after {
  content: "上一页";
}
.zd3-btm /deep/.btn-next:before {
  content: "下一页";
}
.zd3-btm /deep/.btn-prev,
.zd3-btm /deep/.btn-next {
  width: 65px;
	display: inline-flex;
	align-items: center;
	justify-content: center;
}
.app-container {
  padding: 10px;
}
.ellisp{
	width: 100%;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 3;
	overflow: hidden;
	text-align: left;
}
.rolestip {
  width: 100%;
  display: flex;
  justify-content: center;
}
.inps {
  width: 100%;
  height: 32px;
  line-height: 32px;
  padding-left: 10px;
  background: none;
  border: none;
  color: #333;
}
.inps-active {
  width: 100%;
  height: 32px;
  line-height: 32px;
  padding-left: 10px;
  // background:#ccc;
  border: none;
  color: #333;
}
.s-img{
  width: 80px;
  height: 80px;
}
.zd3-title {
	// height: 80px;
	display: flex;
  flex-wrap: wrap;
	align-items: center;
	padding: 20px;
	position: relative;
	background: #ffffff;
	border-radius: 8px;
	border: 1px solid rgba(0, 0, 0, 0.1);

	> p {
		font-size: 14px;
    display: flex;
    color: #333;
    align-items: center;
    margin: 10px 30px 10px 0;
		> span {
			padding-right: 20px;
      width: 80px;
		}

		> input {
			width: 200px;
			height: 32px;
			// background: #ccc;
			border-radius: 3px;
			border: 1px solid #ccc;
			padding-left: 10px;
			color: #333;
		}
	}

	> h3 {
		width: 64px;
		height: 32px;
		background: #18a05e;
		border-radius: 3px;
		margin: 0 30px 0 0;
		color: #fff;
		font-size: 14px;
		line-height: 32px;
		font-weight: 100;
		text-align: center;
		cursor: pointer;
	}

	> h4 {
		width: 64px;
		height: 32px;
		border-radius: 3px;
		border: 1px solid rgba(0, 0, 0, 0.1);
		color: #333;
		font-size: 14px;
		line-height: 32px;
		font-weight: 100;
		text-align: center;
		cursor: pointer;
		margin-right: 30px;
	}
}

.zd3-btm {
  background: rgba(255, 255, 255, 1);
  border-radius: 8px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  margin-top: 10px;
  overflow: hidden;
  &-title {
    display: flex;
    align-items: center;
    height: 70px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    background: #ffffff;
    > p {
      text-align: center;
      font-size: 16px;
      color: #333;
    }
  }
	.el-pagination {
		white-space: nowrap;
		padding: 15px 5px;
		color: #303133;
		font-weight: bold;
	}
  .addDate {
    margin: 20px 32px 10px 32px;
    height: 32px;
    border-radius: 3px;
    text-align: center;
    line-height: 32px;
    font-size: 14px;
    color: #333;
    cursor: pointer;
    border: 1px dashed rgba(0, 0, 0, 0.25);
  }
  .el-button--text {
    margin: 0 10px;
  }
}
.change {
  display: flex;
  flex-wrap: wrap;
  > p {
    width: 50%;
    font-size: 14px;
    display: flex;
    align-items: center;
    color: #333;
    /deep/.el-input__inner {
      width: inherit;
      background: none;
      height: 32px;
      // line-height: 32px;
      color: #333;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding: 0 25px;
    }
    /deep/.el-input__icon {
      line-height: 32px;
    }
    .userselect {
      // max-width: 250px;
      flex: 1;
    }

    > span {
      display: block;
      width: 120px;
      text-align: right;
      padding-right: 15px;
    }
    > input {
      width: inherit;
      flex: 1;
      height: 32px;
      border-radius: 3px;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding-left: 10px;
      color: #333;
    }
  }
}
</style>